package com.imoblife.brainwavestus.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.imoblife.brainwavestus.R;
import com.imoblife.brainwavestus.adapter.LvBaseProductAdapter.ViewHolder;
import com.imoblife.brainwavestus.product.Product;
import com.imoblife.brainwavestus.product.ProductInfo;
import com.imoblife.brainwavestus.util.BitMapFlyweightFactory;
import com.imoblife.brainwavestus.util.ProductUtil;

public class DiscountProductAdapter extends LvBaseProductAdapter {

	public DiscountProductAdapter(Context ctx, int layoutId) {
		super(ctx, layoutId);		
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		ViewHolder vh = null;

		if (convertView == null) {

			convertView = mInflater.inflate(mLayoutId, null);
			
			vh = new ViewHolder();

			vh.ivProductIcon = (ImageView) convertView
					.findViewById(R.id.product_icon);

			vh.tvProductName = (TextView) convertView
					.findViewById(R.id.product_name);
			
			vh.ivRightIcon = (ImageView) convertView
					.findViewById(R.id.iv_right_icon);
			
			vh.ivSaleGroupIcon = (ImageView) convertView
					.findViewById(R.id.iv_discount_price_icon);
			
			vh.tvDiscountPrice = (TextView) convertView
					.findViewById(R.id.new_price);
			vh.tvOriginalPrice = (TextView) convertView
					.findViewById(R.id.old_price);
			
			convertView.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}

		if (mProductData == null) {

			return null;
		}
		
		Product product = mProductData.get(position);

		if (product == null) {
			return null;
		}
		ProductInfo info = product.getProductInfo();
		
		String productName = info.getName();
		String productTitle = info.getTitle();
		vh.ivProductIcon.setImageBitmap(BitMapFlyweightFactory
				.getBitMapFlyweightFactory(mCtx.getAssets())
				.getBitMapFlyweight(productName).getBitmap());
		vh.tvProductName.setText(productTitle);
		vh.ivRightIcon.setImageResource(mRightIconId);
		vh.ivSaleGroupIcon.setImageResource(
				ProductUtil.discount2ImgId(info.getDiscount()));
		vh.tvDiscountPrice.setText(info.getPriceOfDollar());
		vh.tvOriginalPrice.setText(info.getOriginalPrice());
		
		return convertView;
	}
	
	
}
